home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / tvmouse.exe / IBMMOUSE.C < prev    next >
C/C++ Source or Header  |  1993-01-03  |  6KB  |  317 lines

  1. /*
  2.  * IBMMOUSE.C
  3.  *
  4.  *  COPYRIGHT : 1990 Echidna.
  5.  * PROGRAMMER : Gregg A. Tavares
  6.  *    VERSION : 00.000
  7.  *    CREATED : 07/09/90
  8.  *   MODIFIED : 01/03/93
  9.  *       TABS : 05 09
  10.  *
  11.  *         \|///-_
  12.  *         \oo///_
  13.  *    -----w/-w------
  14.  *     E C H I D N A
  15.  *    ---------------
  16.  *
  17.  * DESCRIPTION
  18.  *        IBM Mouse Glue modified from original code by Dan Chang.
  19.  *
  20.  * HISTORY
  21.  *
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <dos.h>
  26. #include "mouse.h"
  27.  
  28. /**************************** C O N S T A N T S ***************************/
  29.  
  30. #define MOUSE_INT 0x33
  31.  
  32. #define TRUE    1
  33. #define FALSE    0
  34.  
  35. /******************************** T Y P E S *******************************/
  36.  
  37. typedef struct {
  38.     unsigned char    Char;
  39.     unsigned char    Attr;
  40. } ScreenElem;
  41.  
  42. /****************************** G L O B A L S *****************************/
  43.  
  44. static void interrupt (*oldvect)(void);
  45.  
  46. static ScreenElem OldData;
  47. static ScreenElem *MouseScreen;
  48.  
  49. static short inside;
  50. static short ShowCount = 0;
  51. static short OldShowCount = 0;
  52. static short MSIntHndl;
  53.  
  54. static short MouseX;
  55. static short MouseY;
  56. static short MouseButtons;
  57.  
  58. static short MouseXMin     = 0;
  59. static short MouseXRange = 80;
  60. static short MouseYMin   = 0;
  61. static short MouseYRange = 25;
  62.  
  63. static short MouseXRes   = 640;
  64. static short MouseYRes   = 200;
  65.  
  66. static short OldMouseX;
  67. static short OldMouseY;
  68.  
  69. static short MouseScrnWidth  = 80;
  70. static short MouseScrnHeight = 25;
  71.  
  72. static short RestoreState = 0;
  73.  
  74. /******************************* M A C R O S ******************************/
  75.  
  76.  
  77. /***************************** R O U T I N E S ****************************/
  78.  
  79. #if 0
  80. INT 33 - MS MOUSE - READ MOTION COUNTERS
  81.     AX = 000Bh
  82. Return: CX = number of mickeys mouse moved horizontally since last call
  83.     DX = number of mickeys mouse moved vertically
  84. Notes:    a mickey is the smallest increment the mouse can sense
  85.     positive values indicate down/right
  86. SeeAlso: AX=0003h,AX=001Bh
  87. #endif
  88.  
  89.  
  90. #if __TURBOC__
  91. #pragma option -N-
  92. #pragma option -r-
  93. #endif
  94.  
  95. static void RestoreMouseArea (void)
  96. {
  97.  
  98.     if (RestoreState)
  99.     {
  100.  
  101.         RestoreState = FALSE;
  102.  
  103.         MouseScreen[OldMouseY * MouseScrnWidth + OldMouseX] = OldData;
  104.     }
  105.  
  106.  
  107. }
  108.  
  109. static interrupt void MouseInterrupt (void)
  110. {
  111.     if (!inside)
  112.     {
  113.         short    mouseOn;
  114.  
  115.         inside = TRUE;
  116.         {
  117.             union REGS regs;
  118.             regs.x.ax = 0x03;
  119.  
  120.             int86(MOUSE_INT, ®s, ®s);
  121.  
  122.             MouseButtons = regs.x.bx & 0x03;
  123.  
  124.             regs.x.ax = 0x0B;
  125.         //    regs.x.ax = 0x27;
  126.  
  127.             int86(MOUSE_INT, ®s, ®s);
  128.  
  129.             MouseX += (short)regs.x.cx;
  130.             MouseY += (short)regs.x.dx;
  131.  
  132.             if (MouseX < 0)          MouseX = 0;
  133.             if (MouseX >= MouseXRes) MouseX = MouseXRes - 1;
  134.             if (MouseY < 0)          MouseY = 0;
  135.             if (MouseY >= MouseYRes) MouseY = MouseYRes - 1;
  136.         }
  137.  
  138.  
  139.         mouseOn = ShowCount > 0;
  140.         if (mouseOn)
  141.         {
  142.             short    x;
  143.             short    y;
  144.             short    oldMouseOn;
  145.             short    doit0;
  146.             short    doit2;
  147.             short    doit3;
  148.  
  149.             x = ((long)MouseX * MouseScrnWidth  / MouseXRes)
  150.             y = ((long)MouseY * MouseScrnHeight / MouseYRes)
  151.  
  152.             oldMouseOn = OldShowCount > 0;
  153.             doit0 = mouseOn != oldMouseOn;
  154.             doit2 = x != OldMouseX;
  155.             doit3 = y != OldMouseY;
  156.             if (doit0 || doit2 || doit3)
  157.             {
  158.                 RestoreMouseArea ();
  159.  
  160.                 //
  161.                 // Save Area
  162.                 //
  163.                 OldData = MouseScreen[y * MouseScrnWidth + x];
  164.                 RestoreState = TRUE;
  165.  
  166.                 //
  167.                 // Draw Mouse
  168.                 //
  169.                 MouseScreen[y * MouseScrnWidth + x].Attr ^= 0x7F;
  170.  
  171.                 //
  172.                 // Save old position
  173.                 //
  174.                 OldMouseX     = x;
  175.                 OldMouseY     = y;
  176.  
  177.             }
  178.         }
  179.         OldShowCount = ShowCount;
  180.         inside = FALSE;
  181.     }
  182.  
  183.     oldvect ();
  184. }
  185.  
  186. void ShowMouse(void)
  187. {
  188.     if (!inside)
  189.     {
  190.         #if 0
  191.         ShowCount++;
  192.         #else
  193.         OldShowCount = 0;
  194.         ShowCount = 1;
  195.         #endif
  196.     }
  197. }
  198.  
  199. void HideMouse(void)
  200. {
  201.     if (!inside)
  202.     {
  203.         #if 0
  204.         ShowCount--;
  205.         if (!ShowCount) {
  206.             RestoreMouseArea ();
  207.             OldShowCount = 0;
  208.         }
  209.         #else
  210.         RestoreMouseArea ();
  211.         OldShowCount = 0;
  212.         ShowCount = 0;
  213.         #endif
  214.     }
  215. }
  216.  
  217. void ReadMouse(MouseInfo *m)
  218. {
  219.     m->X          = MouseXMin + (long)MouseX * (long)MouseXRange / MouseXRes;
  220.     m->Y          = MouseYMin + (long)MouseY * (long)MouseYRange / MouseYRes;
  221.     m->OldButtons = m->Buttons;
  222.     m->Buttons    = MouseButtons;
  223. }
  224.  
  225. #if __TURBOC__
  226. #pragma option -r.
  227. #pragma option -N.
  228. #endif
  229.  
  230. void SetMousePosition (short x, short y)
  231. {
  232.     MouseX = (long)(x - MouseXMin) * MouseXRes / MouseXRange;
  233.     MouseY = (long)(y - MouseYMin) * MouseYRes / MouseYRange;
  234. }
  235.  
  236. void SetMouseResolution (short xres, short yres)
  237. {
  238.     MouseXRes = xres;
  239.     MouseYRes = yres;
  240. }
  241.  
  242. void SetMouseRange (short xmin, short xmax, short ymin, short ymax)
  243. {
  244.     MouseXMin   = xmin;
  245.     MouseYMin   = ymin;
  246.     MouseXRange = xmax - xmin + 1;
  247.     MouseYRange = ymax - ymin + 1;
  248. }
  249.  
  250. void SetMouseScreenDimensions (short width, short height)
  251. {
  252.     MouseScrnWidth  = width;
  253.     MouseScrnHeight = height;
  254. }
  255.  
  256. short OpenMouse(void)
  257. {
  258.     union REGS regs;
  259.     unsigned char far *hMouse;
  260.     short    result = FALSE;
  261.  
  262.     if (MSIntHndl)
  263.     {
  264.         puts ("Error: Mouse already open");
  265.     }
  266.  
  267.     MouseScreen = MK_FP (0xB800, 0x0000);
  268.  
  269.     regs.x.ax = 0x00;
  270.  
  271.     if ((hMouse = *(unsigned char * far *) MK_FP(0x0, MOUSE_INT*4)) == 0) {
  272.         puts ("\nMouse Driver not found");
  273.         return FALSE;
  274.     }
  275.     if (*hMouse == 0xCF /* IRET */) {
  276.         puts ("\nMouse Driver not found");
  277.         return FALSE;
  278.     }
  279.  
  280.     int86(MOUSE_INT, ®s, ®s);
  281.  
  282.     result = (regs.x.ax == (unsigned int)(-1));
  283.  
  284.     if (result)
  285.     {
  286.         oldvect = getvect (0x08);
  287.         setvect (0x08, MouseInterrupt);
  288.         MSIntHndl = TRUE;
  289.         if (MSIntHndl) {
  290.             return TRUE;
  291.         }
  292.         CloseMouse ();
  293.     }
  294.  
  295.     return FALSE;
  296. }
  297.  
  298. void CloseMouse (void)
  299. {
  300.     if (!MSIntHndl)
  301.     {
  302.         puts ("Error: Mouse not installed");
  303.     }
  304.     {
  305.         if (MSIntHndl)    setvect (0x08, oldvect);
  306.         MSIntHndl = 0;
  307.  
  308.         {
  309.             union REGS regs;
  310.             regs.x.ax = 0x00;
  311.  
  312.             int86(MOUSE_INT, ®s, ®s);
  313.         }
  314.     }
  315. }
  316.  
  317.